package com.frank.libandroid;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class BaseVMFragment<V extends ViewDataBinding, VM extends BaseViewModel> extends BaseFragment<V> {
    protected VM mViewModel;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Class<VM> vmClass = (Class<VM>) ReflectUtil.getGeneric(this, 1);
        mViewModel = new ViewModelProvider(this).get(vmClass);
        super.onViewCreated(view, savedInstanceState);
    }

    protected <T> void watch(Key<T> key, Observer<T> observer) {
        LiveDataBus.with(key).observe(this, observer);
    }
}
